<template>
  <div class="star-coin-category">
    <!-- 搜索栏 -->
    <div class="search-block">
      <div class="item-msg" v-if="$store.state.userInfo.group === 'administrator'">
        <label>钱包</label>
        <el-select v-model="searchForm.platform_id" placeholder="全部" size="mini" @change="search()">
          <el-option v-for="item in wallets" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </div>
      <div class="item-msg">
        <label>类型</label>
        <el-select
          v-model="searchForm.type"
          placeholder="全部"
          size="mini"
          clearable
          @change="search()"
        >
          <el-option :value="1" label="去中心化钱包"></el-option>
          <el-option :value="2" label="托管钱包"></el-option>
        </el-select>
      </div>
      <!-- <el-button class="search-btn" @click="search()" size="mini" type="primary">查询</el-button> -->
      <el-button
        class="add-btn"
        size="mini"
        type="primary"
        @click="editData.type=0;showDialog=true"
      >+ 添加币种分类</el-button>
    </div>
    <!-- 数据表 -->
    <div class="data-table">
      <el-table
        size="small"
        v-loading="loading"
        element-loading-text="拼命加载中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(211, 215, 212, 0.5)"
        :data="tableData"
        stripe
        header-cell-class-name="headerStyle"
        style="width: 100%"
      >
        <el-table-column align="center" prop="name.zh-CN" label="分类名称" width="320"></el-table-column>
        <el-table-column align="center" prop="sort" label="排序"></el-table-column>
        <el-table-column align="center" label="操作" width="180">
          <template slot-scope="item">
            <el-button type="text" @click="editCategory(item.row)" class="edit-btn">编辑</el-button>
            <el-button type="text" @click="delCategory(item.row)" class="del-btn">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 查看资产弹窗 -->
      <transition name="fade">
        <add-category
          v-if="showDialog"
          @cancel="showDialog=false"
          @success="showDialog=false;search()"
          :platform="searchForm.platform_id"
          :detail="editData"
        ></add-category>
      </transition>
    </div>
  </div>
</template>

<script>
import Pages from "@/components/PageBox.vue";
import AddCategory from "@/components/CoinsManage/AddCategory.vue";
import { getStarCategory, delStarCategory } from "./Coins.service.js";
export default {
  components: {
    Pages,
    AddCategory
  },
  computed: {
    wallets() {
      return this.$store.state.wallet_list || [];
    }
  },
  data() {
    return {
      loading: false,
      searchForm: {
        platform_id: String(this.$store.state.userInfo.platform_id),
        type: ""
      },
      tableData: [],
      showDialog: false,
      editData: {
        type: 0, //0——添加，1——编辑
        data: {}
      }
    };
  },

  mounted() {
    this.search();
  },
  methods: {
    async search() {
      this.loading = true;
      const res = await getStarCategory(this.searchForm);
      this.tableData = res.data.items || [];
      this.loading = false;
    },
    editCategory(item) {
      this.editData.type = 1;
      this.editData.data = item;
      this.showDialog = true;
    },
    delCategory(item) {
      this.$confirm(
        `是否删除 <span class="confirm-tip-color">${
          item.name["zh-CN"]
        }</span> 分类?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          dangerouslyUseHTMLString: true
        }
      )
        .then(async () => {
          const res = await delStarCategory({
            id: item.id
          });
          if (res.code === 0) {
            this.$message.success("删除成功!");
            this.search();
          }
        })
        .catch(() => this.$message.info("已取消删除"));
    }
  }
};
</script>

<style lang="scss" scoped>
.star-coin-category {
  // position: relative;
  min-height: calc(100vh - 100px);
  .search-block {
    width: 100%;
    padding: 40px 30px;
    border-radius: 5px;
    background: #fff;
    box-shadow: rgba(0, 0, 0, 0.1) 0 0 15px;
    .add-btn {
      margin-left: 15px;
    }
    .item-msg {
      display: inline-block;
      margin-right: 20px;
      font-size: 1 size= "medium" 4;
      color: $admin_font_param;
      label {
        display: inline-block;
        max-width: 90px;
        text-align: right;
        margin-right: 10px;
      }
      .el-input {
        width: 150px;
      }
      .el-select {
        width: 150px;
      }
    }
    .search-btn {
      margin-left: 25px;
    }
  }
  .data-table {
    // position: relative;
    background: #fff;
    border-radius: 5px;
    margin-top: 30px;
    box-shadow: rgba(0, 0, 0, 0.1) 0 0 15px;
    .del-btn {
      color: #f56c6c;
    }
  }
  .fade-enter-active,
  .fade-leave-active {
    transition: opacity 0.3s;
  }
  .fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
    opacity: 0;
  }
}
</style>